package net.xo68.destiny.manager.mapper;


import net.xo68.destiny.manager.entity.DistributedLock;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/** 
 * DistributedLock mybatis mapper
 * @author wuxie(16349023@qq.com)
 * @date 2020年12月17日 14:16:49
 *
 */
public interface DistributedLockMapper {


	
	@Select("SELECT * FROM destiny_distributed_lock WHERE lock_key = #{lockKey}")
    @Results(value = { 
    @Result(id = true, column = "lock_key", property = "lockKey"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "lock_status", property = "lockStatus"),
        @Result(column = "lock_hold", property = "lockHold"),
        @Result(column = "lock_expire", property = "lockExpire"),
        @Result(column = "lock_period", property = "lockPeriod")
    })
    DistributedLock find(@Param("lockKey") String lockKey);
	
	@Select("SELECT * FROM destiny_distributed_lock")
    @Results(value = { 
    @Result(id = true, column = "lock_key", property = "lockKey"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "lock_status", property = "lockStatus"),
        @Result(column = "lock_hold", property = "lockHold"),
        @Result(column = "lock_expire", property = "lockExpire"),
        @Result(column = "lock_period", property = "lockPeriod")
    })
    List<DistributedLock> list();
	
	@Insert("insert into destiny_distributed_lock (lock_key ,create_time ,lock_status ,lock_hold ,lock_expire ,lock_period ) values (#{lockKey} , #{createTime} , #{lockStatus} , #{lockHold} , #{lockExpire} , #{lockPeriod}  )")
    @Options(useGeneratedKeys = true, keyProperty = "lockKey")
    int insert(DistributedLock distributedLock);
	
	@Update({"update destiny_distributed_lock set",
      " create_time=#{createTime}, lock_status=#{lockStatus}, lock_hold=#{lockHold}, lock_expire=#{lockExpire}, lock_period=#{lockPeriod} ",
      "where lock_key = #{lockKey}"
    })
    int update(DistributedLock distributedLock);

    @Update({"update destiny_distributed_lock set",
            " lock_status=#{lockStatus}, lock_expire=NOW()",
            "where lock_key = #{lockKey}"
    })
    int updateStatus(@Param("lockKey") String lockKey,@Param("lockStatus") String lockStatus);

    /**
     * 移除过期的运行状态锁信息
     * @param lockKey
     * @param expire
     * @return
     */
    @Delete("delete FROM destiny_distributed_lock WHERE lock_key = #{lockKey} and lock_status='RUN' and lock_Expire <= #{expire}")
    int deleteExpired(@Param("lockKey") String lockKey,@Param("expire") Date expire);

    /**
     * 移除周期过期的锁信息
     * @param lockKey
     * @param expire
     * @return
     */
    @Delete({"delete FROM destiny_distributed_lock",
            " WHERE lock_key = #{lockKey} and lock_period > 0 and lock_status='SUCCESS'",
            " and lock_Expire <= DATE_ADD(#{expire},INTERVAL -lock_period SECOND) "})
    int deleteExpiredPeriod(@Param("lockKey") String lockKey,@Param("expire") Date expire);

	@Delete("delete FROM destiny_distributed_lock WHERE lock_key = #{lockKey}")
	int delete(@Param("lockKey") String lockKey);
}